package com.eurasia.assignment.infrastructure.entity;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import com.eurasia.assignment.domain.ProjectAssignment;
import com.eurasia.assignment.domain.TeamMember;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 项目分配实体
 */
@Entity
@Table(name = "project_assignments")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProjectAssignmentEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "project_id", nullable = false)
    private Long projectId;
    
    @Column(name = "create_time", nullable = false)
    private LocalDateTime createTime;
    
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;
    
    @OneToMany(mappedBy = "assignment", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private Set<TeamMemberEntity> teamMembers = new HashSet<>();
    
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id", referencedColumnName = "id", insertable = false, updatable = false)
    private ProjectEntity project;
    
    /**
     * 添加团队成员
     */
    public void addTeamMember(TeamMemberEntity teamMember) {
        teamMembers.add(teamMember);
        teamMember.setAssignment(this);
    }
    
    /**
     * 移除团队成员
     */
    public void removeTeamMember(TeamMemberEntity teamMember) {
        teamMembers.remove(teamMember);
        teamMember.setAssignment(null);
    }
    
    /**
     * 从领域模型创建实体
     */
    public static ProjectAssignmentEntity fromDomain(ProjectAssignment domain) {
        ProjectAssignmentEntity entity = new ProjectAssignmentEntity();
        entity.setId(domain.getId());
        entity.setProjectId(domain.getProjectId());
        entity.setCreateTime(domain.getCreateTime());
        entity.setUpdateTime(domain.getUpdateTime());
        
        // 转换团队成员
        if (domain.getTeamMembers() != null) {
            for (TeamMember member : domain.getTeamMembers()) {
                TeamMemberEntity memberEntity = TeamMemberEntity.fromDomain(member);
                entity.addTeamMember(memberEntity);
            }
        }
        
        return entity;
    }
    
    /**
     * 转换为领域模型
     */
    public ProjectAssignment toDomain() {
        ProjectAssignment domain = new ProjectAssignment();
        domain.setId(this.id);
        domain.setProjectId(this.projectId);
        domain.setCreateTime(this.createTime);
        domain.setUpdateTime(this.updateTime);
        
        // 转换团队成员
        if (this.teamMembers != null) {
            domain.setTeamMembers(
                this.teamMembers.stream()
                    .map(TeamMemberEntity::toDomain)
                    .collect(Collectors.toSet())
            );
        }
        
        return domain;
    }
} 